<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Support\Facades\Crypt;
use Validator;
use Session;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Org\code\Code;
use Illuminate\Support\Facades\Hash;
use App\Model\User;
//验证码二
use Gregwar\Captcha\CaptchaBuilder;
use Gregwar\Captcha\PhraseBuilder;

class LoginController extends Controller
{
    //后台登录页
    public function login(){
        return view('admin.login');
    }

    //后台首页
    public function adminIndex(){
        return view('layouts.admin');
    }

    //后台欢迎页面
    public function welcome(){
        return view('admin.welcome');
    }

    //验证码
    public function code(){

//        $code = new Code();
//        return $code->make();
        $builder = new CaptchaBuilder();
        $builder->build($width = 100, $height = 40, $font = null);
        $phrase = $builder->getPhrase();
        Session::flash('milkcaptcha', $phrase);
        $builder->output();
       /* CaptchaBuilder::create()
            ->build()
            ->getPhrase()
            ->output();*/
//        return $phraseBuilder;
    }

    //处理用户登录方法
    public function doLogin(Request $request){
        //1.接收表单数据
        $input = $request->except('_token');

        $rule = [
            'username'=>'required|between:4,18',
            'password'=>'required|between:4,18|alpha_dash',
        ];

        $msg = [
            'username.required'=>'用户名必须输入',
            'username.between'=>'用户名长度必须在4-18位之间',
            'password.required'=>'密码必须输入',
            'password.between'=>'密码长度必须在4-18位之间',
            'password.alpha_dash'=>'密码必须是数组字母下划线'
        ];
        //2.验证数据
        $validator = Validator::make($input,$rule,$msg);

        if ($validator->fails()) {
            return redirect('admin/login')
                ->withErrors($validator)
                ->withInput();
        }
        //3.验证是否此用户 (用户名,密码,验证码)

        //var_dump($input['code'],Session::get('milkcaptcha'));die;
        if (strtoupper(Session::get('milkcaptcha')) != strtoupper($input['code'])) {
            //用户输入验证码错误
            return  redirect('admin/login')->with('errors','您输入验证码错误');
        }

        $user = User::where('user_name',$input['username'])->first();
        if(!$user){
            return redirect('admin/login')->with('errors','用户名错误');

        }

        if($input['password']!= Crypt::decrypt($user->user_pass)){
            return redirect('admin/login')->with('errors','密码错误');
        }
        //4.保存用户信息到session中
        session()->put('user',$user);

        //5.跳转到后台首页
        return redirect('admin/index');
       /* //1.接收表单数据
        $input = $request->except('_token');
//        var_dump($input);die;
        $rule = [
            'username'=>'required|between:4,18',
            'password'=>'required|between:4,18|alpha_dash',
        ];
        //2,进行表单验证
        $validator = Validator::make($input,$rule);
        if($validator->fails()){
            return redirect('admin/login')
                ->withErrors($validator)
                ->widthInput();
        }*/

    }

    //退出登录
    public function logout(){
        session()->flush();
        return redirect('admin/login');
    }



    //加密算法
    public function jiami(){
        //1.md5加密
//        $str = 'salt'.'123456';
//        return md5($str);
        //2.哈希加密
        /*$str1 = '123456';
        $hash = Hash::make($str1);
        if(Hash::check($str1,$hash)){
            return '密码正确';
        }else{
            return '密码错误';
        }*/

        //3.crypt加密
        $str2 = '123456';
        //$crypt_str = Crypt::encrypt($str2);
        $crypt_str = 'eyJpdiI6IlFNTVBFY0RPWlVBR0YyVW1rbHkwTGc9PSIsInZhbHVlIjoiYlc2cUFQRXJaZG9PUzdoYVluU0ttUT09IiwibWFjIjoiZGI5MDZmOGJkZTg0Mzk1YThiZDZiMTA1YmNjMmRlMmUyMzQwYjdmNzYyOTZjNTllYzY1MWFjOTY2MWRlNjJmOCJ9';
        //return $crypt_str;

        if(Crypt::decrypt($crypt_str)==$str2){
            return "密码正确";
        }



    }


















}
